/********************************************************************************
* @author: Ternura_xiang
* @email: 2022101883@nit.edu.cn
* @date: Apr 4, 2025
* @version: 1.0
* @description: ST7789屏幕驱动代码
********************************************************************************/

#ifndef __ST7789_H__
#define __ST7789_H__

#include "main.h"
#include "spi.h"


/* 移植指南 */
// 1.更改 SPI宏定义，并重写LCD_SPI_Transmit
// 2.更改 IO端口，并重写LCD_XX_SET函数
// 3.重写LCD_Delay
// 4.根据需要更改LCD尺寸以及显示刷新方向


// SPI宏定义
#define LCD_SPI hspi2
#define LCD_SPI_Transmit(hspi, pData, Size, Timeout)   HAL_SPI_Transmit(hspi, pData, Size, Timeout)

// IO宏定义
#define LCD_CS_PORT GPIOB
#define LCD_CS_PIN GPIO_PIN_11
#define LCD_DC_PORT GPIOB
#define LCD_DC_PIN GPIO_PIN_14
#define LCD_RES_PORT GPIOB
#define LCD_RES_PIN GPIO_PIN_12

#define LCD_CS_SET  HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_SET)
#define LCD_CS_RESET HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, GPIO_PIN_RESET)
#define LCD_DC_SET  HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_SET)
#define LCD_DC_RESET HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, GPIO_PIN_RESET)
#define LCD_RES_SET  HAL_GPIO_WritePin(LCD_RES_PORT, LCD_RES_PIN, GPIO_PIN_SET)
#define LCD_RES_RESET HAL_GPIO_WritePin(LCD_RES_PORT, LCD_RES_PIN, GPIO_PIN_RESET)

#define LCD_Delay(delay)    HAL_Delay(delay)
/* LCD设置参数宏定义 */
// LCD尺寸宏定义
#define LCD_WIDTH 240
#define LCD_HEIGHT 320
// LCD显示方向
#define LCD_MY  0   // 上下反转
#define LCD_MX  1   // 水平翻转
#define LCD_MV  1   // 90度旋转
#define LCD_ML  0   // 刷新方向由上至下(0), 由下至上(1)
#define LCD_BGR 0   // 0-> RGB, 1-> BGR
#define LCD_MH  0   // 刷新方向由左至右(0), 由右至左(1)

/* 颜色 */
#define COLOR_BLACK          0x0000      /*   0,   0,   0 */
#define COLOR_WHITE          0xFFFF      /* 255, 255, 255 */
#define COLOR_BLUE           0x001F      /*   0,   0, 255 */
#define COLOR_GREEN          0x07E0      /*   0, 255,   0 */
#define COLOR_RED            0xF800      /* 255,   0,   0 */
#define COLOR_NAVY           0x000F      /*   0,   0, 128 */
#define COLOR_DARKBLUE       0x0011      /*   0,   0, 139 */
#define COLOR_DARKGREEN      0x03E0      /*   0, 128,   0 */
#define COLOR_DARKCYAN       0x03EF      /*   0, 128, 128 */
#define COLOR_CYAN           0x07FF      /*   0, 255, 255 */
#define COLOR_TURQUOISE      0x471A      /*  64, 224, 208 */
#define COLOR_INDIGO         0x4810      /*  75,   0, 130 */
#define COLOR_DARKRED        0x8000      /* 128,   0,   0 */
#define COLOR_OLIVE          0x7BE0      /* 128, 128,   0 */
#define COLOR_GRAY           0x8410      /* 128, 128, 128 */
#define COLOR_GREY           0x8410      /* 128, 128, 128 */
#define COLOR_SKYBLUE        0x867D      /* 135, 206, 235 */
#define COLOR_BLUEVIOLET     0x895C      /* 138,  43, 226 */
#define COLOR_LIGHTGREEN     0x9772      /* 144, 238, 144 */
#define COLOR_DARKVIOLET     0x901A      /* 148,   0, 211 */
#define COLOR_YELLOWGREEN    0x9E66      /* 154, 205,  50 */
#define COLOR_BROWN          0xA145      /* 165,  42,  42 */
#define COLOR_DARKGRAY       0x7BEF      /* 128, 128, 128 */
#define COLOR_DARKGREY       0x7BEF      /* 128, 128, 128 */
#define COLOR_SIENNA         0xA285      /* 160,  82,  45 */
#define COLOR_LIGHTBLUE      0xAEDC      /* 172, 216, 230 */
#define COLOR_GREENYELLOW    0xAFE5      /* 173, 255,  47 */
#define COLOR_SILVER         0xC618      /* 192, 192, 192 */
#define COLOR_LIGHTGRAY      0xC618      /* 192, 192, 192 */
#define COLOR_LIGHTGREY      0xC618      /* 192, 192, 192 */
#define COLOR_LIGHTCYAN      0xE7FF      /* 224, 255, 255 */
#define COLOR_VIOLET         0xEC1D      /* 238, 130, 238 */
#define COLOR_AZUR           0xF7FF      /* 240, 255, 255 */
#define COLOR_BEIGE          0xF7BB      /* 245, 245, 220 */
#define COLOR_MAGENTA        0xF81F      /* 255,   0, 255 */
#define COLOR_TOMATO         0xFB08      /* 255,  99,  71 */
#define COLOR_GOLD           0xFEA0      /* 255, 215,   0 */
#define COLOR_ORANGE         0xFD20      /* 255, 165,   0 */
#define COLOR_SNOW           0xFFDF      /* 255, 250, 250 */
#define COLOR_YELLOW         0xFFE0      /* 255, 255,   0 */


/* 寄存器宏定义 */
#define LCD_NOP         0x00    // No Operation
#define LCD_SLPIN       0x10    // Sleep IN
#define LCD_SLPOUT      0x11    // Sleep OUT
#define LCD_PTLON       0x12    // Partial Display On
#define LCD_NORON       0x13    // Normal Display On
#define LCD_INVOFF      0x20    // Display Inversion Off
#define LCD_INVON       0x21    // Display Inversion On
#define LCD_ALLPOFF     0x22    // All Pixels OFF
#define LCD_ALLPON      0x23    // All Pixels ON
#define LCD_DISPOFF     0x28    // Display Off
#define LCD_DISON       0x29    // Display ON
#define LCD_CASET       0x2A    // Column Address Set
#define LCD_PASET       0x2B    // Page Address Set
#define LCD_RAMWR       0x2C    // Memory Write
#define LCD_RAMRD       0x2E    // Memory Read
#define LCD_PLTAR       0x30    // Partial Area
#define LCD_VSCRDEF     0x33    // Vertical Scrolling Definition
#define LCD_TEOFF       0x34    // Tearing Effect Line Off
#define LCD_TEON        0x35    // Tearing Effect Line ON
#define LCD_MADCTL      0x36    // Memory Access Control
#define LCD_COLMOD      0x3A    // Color Mode
#define LCD_IFMODE      0xB0    // Interface Mode Control
#define LCD_FRMCTR1     0xB1    // Frame Rate Control(In Normal Mode/Full Colors)
#define LCD_FRMCTR2     0xB2    // Frame Rate Control(In Idle Mode/8 Colors)
#define LCD_FRMCTR3     0xB3    // Frame Rate Control(In Partial Mode/Full Colors)
#define LCD_INVTR       0xB4    // Display Inversion Control
#define LCD_PRCTR       0xB5    // Blanking Porch Control
#define LCD_DISCTRL     0xB6    // Display Function Control
#define LCD_ETMOD       0xB7    // Entry Mode Set
#define LCD_CECTRL1     0xB9    // Color Enhancement Control 1
#define LCD_CECTRL2     0xBA    // Color Enhancement Control 2
#define LCD_HSLCTRL     0xBE    // HS Lanes Control
#define LCD_PWRCTRL1    0xC0    // Power Control 1
#define LCD_PWRCTRL2    0xC1    // Power Control 2
#define LCD_PWRCTRL3    0xC2    // Power Control 3
#define LCD_PWRCTRL4    0xC3    // Power Control 4
#define LCD_PWRCTRL5    0xC4    // Power Control 5
#define LCD_VCOMCTRL    0xC5    // VCOM Control
#define LCD_CABCCTRL1   0xC6    // CABC Control 1
#define LCD_CABCCTRL2   0xC8    // CABC Control 2
#define LCD_CABCCTRL3   0xC9    // CABC Control 3
#define LCD_CABCCTRL4   0xCA    // CABC Control 4
#define LCD_CABCCTRL5   0xCB    // CABC Control 5
#define LCD_CABCCTRL6   0xCC    // CABC Control 6
#define LCD_CABCCTRL7   0xCD    // CABC Control 7
#define LCD_CABCCTRL8   0xCE    // CABC Control 8
#define LCD_CABCCTRL9   0xCF    // CABC Control 9
#define LCD_NVMWR       0xD0    // NV Memory Write
#define LCD_NVMPKEY     0xD1    // NV Memory Protection Key
// #define LCD_DISON       0xD2    // NV Memory Status Read
#define LCD_RDID4       0xD3    // Read ID4
#define LCD_AdjustCTRL1 0xD7    // Adjust Control 1
#define LCD_Read_IDV    0xD8    // Read ID Version
#define LCD_PGAMCTRL    0xE0    // Positive Gamma Control
#define LCD_NGAMCTRL    0xE1    // Negative Gamma Control
#define LCD_DGAMCTRL1   0xE2    // Ditigal Gamma Control 1
#define LCD_DGAMCTRL2   0xE3    // Ditigal Gamma Control 2
#define LCD_SETIMAGE    0xE9    // Set Image Function
#define LCD_AdjustCTRL2 0xF2    // Adjust Control 2
#define LCD_AdjustCTRL3 0xF7    // Adjust Control 3
#define LCD_AdjustCTRL4 0xF8    // Adjust Control 4
#define LCD_AdjustCTRL5 0xF9    // Adjust Control 5

/* LCD显示函数 */
void LCD_Init(void);
uint16_t RGB565(uint8_t red, uint8_t green, uint8_t blue);
void LCD_FullArea(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, uint16_t color);
void LCD_Full(uint16_t color);
void LCD_DrawPixel(uint16_t x, uint16_t y, uint16_t color);
void LCD_DrawLine(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, uint16_t width, uint16_t color);
void LCD_DrawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint8_t size);
void LCD_DrawStr(uint16_t x, uint16_t y, char *str, uint16_t color, uint8_t size);
void LCD_DrawHistogram(uint16_t x_start, uint16_t y_start, uint16_t x_length, uint16_t y_length,\
    uint16_t background_color, uint16_t *data, uint16_t data_length,uint16_t data_max, \
    uint16_t data_color, uint16_t axis_width, uint16_t axis_color);

#endif
